<!DOCTYPE html>
<html>
    {include file="Widgets/head" /}
    
    <body class="gray-bg">
        <div class="wrapper wrapper-content animated fadeInUp ibox-content">
            <form action="{:url('Assets/doAdd')}" method="post" class="form-horizontal">
                <input type="hidden" id="interest_rate_base">
                <input type="hidden" id="type" name="type" value="{$type}">
                <div class="row">
                    <div class="col-sm-12 ">
                        <div class="ibox-title">
                            <h3>企业贷-新增</h3>
                            <a class="pull-right" href="javascript:void(0);" onclick="window.history.back()">
                                <button class="btn btn-primary" type="button">返回</button>
                            </a>
                        </div>
                    </div>
                </div>
                <ul id="myTab" class="nav nav-tabs">
                    <li id="li-first" class="active"><a href="#home1" data-toggle="tab">第一步</a></li>
                    <li id="li-next"><a href="#home2" data-toggle="tab">第二步</a></li>
                </ul>
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane fade in active" style="padding-top: 20px" id="home1">
                        <div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label" for="batch_number">批次号：</label>
                                <div class="col-sm-2 m-b-xs">
                                    <input type="text" id="batch_number" name="batch_number" value="{$batch_number}" class="form-control input-s-sm inline" placeholder="批次号">
                                </div>
                                <label class="col-sm-1 control-label" for="duration">放款期限：</label>
                                <div class="input-group col-sm-2 m-b-xs">
                                    <select id="duration" name="duration" class="form-control input-s-sm inline">
                                        {foreach $term_list as $term} 
                                        <option value="{$term}" {if($term == $duration)} selected {/if}>{$term}</option>
                                        {/foreach}
                                    </select>
                                    <span class="input-group-addon">天</span>
                                </div> 
                                <label class="col-sm-1 control-label" for="repayment_type">还款方式：</label>
                                <div class="col-sm-2 m-b-xs">
                                    <select id="repayment_type" name="repayment_type" class="form-control input-s-sm inline">
                                        {foreach $repayment_type_list as $key => $val} 
                                        <option value="{$key}" {if ($repayment_type == $key)} selected {/if}>{$val}</option>
                                        {/foreach}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label" for="left_money_min">在贷余额：</label>
                                <div class="col-sm-2">
                                    <input type="text" id="left_money_min" name="left_money_min" class="form-control" 
                                           value="{$left_money_min}">
                                </div>
                                <label class="pull-left control-label col-sm-1" style="text-align:center;" for="left_money_max">—</label>
                                <div class="col-sm-2">
                                    <input type="text" id="left_money_max" name="left_money_max" class="form-control"
                                           value="{$left_money_max}">
                                </div>
                                <div class="col-sm-4 col-sm-offset-2">
                                    <button class="btn btn-primary" type="button" id="btn-search">查询</button>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="pull-left control-label col-sm-1">资产本金：</label>
                                <div class="col-sm-1">
                                    <input type="number" id="capital" name="capital" class="form-control input-s-sm inline" required="required" readonly="readonly" required="required">
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-sm-12">
                                    <table class="table table-bordered">
                                        <thead>
                                            <tr>
                                                <th><input id="all_user_ids" name="all_user_ids" type="checkbox"/>编号</th>
                                                <th>批次号</th>
                                                <th>企业名称</th>
                                                <th>联系人号码</th>
                                                <th>借款金额</th>
                                                <th>剩余期限（天）</th>
                                                <th>借款期限（天）</th>
                                                <th>放款期限（天）</th>
                                                <th>在贷余额</th>
                                                <th>放款金额</th>
                                            </tr>
                                        </thead>
                                        <tbody id="borrowerList">
                                            
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>             
                    <div class="tab-pane fade in"  style="padding-top: 20px" id="home2">
                        <div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label" for="code">资产编号：</label>
                                <div class="col-sm-2 m-b-xs">
                                    <input type="text" id="code" name="code" value ="{$code}" class="form-control input-s-sm inline" readonly="readonly">
                                </div>
                            </div>
                            <!--
                            <div class="form-group">
                                <label class="col-sm-1 control-label" for="duration">借款期限：</label>
                                <div class="input-group col-sm-2 m-b-xs">
                                    <input type="text" id="duration" name="duration" value="" class="form-control input-s-sm inline" placeholder="180" readonly>
                                    <span class="input-group-addon">天</span>
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label" for="capital">资产本金：</label>
                                <div class="col-sm-2 m-b-xs">
                                    <input type="text" id="capital" name="capital" value="" class="form-control input-s-sm inline" readonly>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label" for="repayment_type">还款方式：</label>
                                <div class="col-sm-2 m-b-xs">
                                    <select id="repayment_type" name="repayment_type" class="form-control input-s-sm inline" readonly
                                            onfocus="this.defaultIndex=this.selectedIndex;" onchange="this.selectedIndex=this.defaultIndex;">
                                        <option value="1">到期还本息</option>
                                        <option value="2">按月付息，到期还本</option>
                                        <option value="3">按季付息，到期还本</option>
                                        <option value="4">等额本息</option>
                                    </select>
                                </div>
                            </div>
                            -->
                            <div class="form-group">
                                <label class="pull-left control-label col-sm-1" for="contract_time_start">合同起止时间：</label>
                                <div class="col-sm-2">
                                    <input type="date" id="contract_time_start" name="contract_time_start" class="form-control laydate-icon input-s-sm inline layer-date"
                                           onclick="laydate({istime: false, format: 'YYYY-MM-DD'})" required="required">
                                </div>
                                <label class="pull-left control-label col-sm-1" style="text-align:center;" for="contract_time_end">-</label>
                                <div class="col-sm-2">
                                    <input type="date" id="contract_time_end" name="contract_time_end" class="form-control laydate-icon input-s-sm inline layer-date"
                                           onclick="laydate({istime: false, format: 'YYYY-MM-DD'})" required="required">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label" for="name">资产名称：</label>
                                <div class="col-sm-2">
                                    <input type="text" id="name" name="name" class="form-control" required="required">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label" for="interest_rate">总成本利率：</label>
                                <div class="input-group col-sm-2 m-b-xs">
                                    <input type="number" id="interest_rate" name="interest_rate" min="0.01" step="0.01" class="form-control input-s-sm inline" required="required">
                                    <span class="input-group-addon">%</span>
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="pull-left control-label col-sm-1" for="marketing_fee">营销费率：</label>
                                <div class="input-group col-sm-2 m-b-xs">
                                    <input type="number" id="marketing_fee" name="marketing_fee" min="0" step="0.01" class="form-control input-s-sm inline">
                                    <span class="input-group-addon">%</span>
                                </div> 
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label" for="service_remarks">营销备注：</label>
                                <div class="col-sm-2">
                                    <input type="text" id="service_remarks" name="service_remarks" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-1 control-label" for="service_amount_type">收取方式：</label>
                                <div class="col-sm-2 m-b-xs">
                                    <select id="service_amount_type" name="service_amount_type" class="form-control input-s-sm inline">
                                        <option value="1" >线上结算</option>
                                        <option value="2" >线下打款</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-1">
                                    <button class="btn btn-primary" type="button" id="btn_tj">提交</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
<!--                <div class="row text-center">
                    <div class="col-sm-4 col-sm-offset-1">
                        <a href="javascript:void(0);" onclick="window.history.back()">
                            <button class="btn btn-primary" type="button">返回</button>
                        </a>
                        <button class="btn btn-primary" type="submit">提交</button>
                    </div>
                </div>-->
            </form>
        </div>
    </body>
    <script>
        //点击全选和取消全选
        $(function () {
            $("#all_user_ids").click(function () {
                $("input[name='user_ids[]']:checkbox").prop("checked", this.checked);
                check_click();
            });
            //单选事件
            $(document).on("click",".checkbox",function(e){
                check_click();
            });
            //提交
            $('#btn_tj').click(function () {
                //总成本利率判断
                var interest_rate_base = $("#interest_rate_base").val();
                var interest_rate = $("#interest_rate").val();
                if (parseFloat(interest_rate) < parseFloat(interest_rate_base)) {
                    alert('总成本利率不能低于默认值:'+interest_rate_base);
                    return false;
                }
                
                //提交时，借款人必须选择
                var capital = $("#capital").val();
                if (capital == '' || capital == 0){
                    alert('请选择借款人');
                    return false;
                }
                $('form').submit();
            });
        });
        //计算资产本金
        function check_click(){
            var amountMoney = 0;
            setTimeout(function(){
                $(".checkbox").each(function () {
                  if($(this).is(':checked') ){                     
                      var ThisVAlue = $(this).parent().parent().children("td:eq(9)").html()
                      amountMoney =  parseInt(ThisVAlue) + parseInt(amountMoney);
                  }
                })
               $("input[name='capital']").val(amountMoney);
            },200);
        } 
        $(function () {
            //查询按钮，填充数据
            $("#btn-search").click(function () {
                batch_number = $('#batch_number').val();
                duration = $('#duration').val();
                if (duration == ''){
                    alert('请填写期限');
                    return;
                }
                repayment_type = $('#repayment_type').val();
                left_money_min = $('#left_money_min').val();
                left_money_max = $('#left_money_max').val(); 
                type = $('#type').val();
                
                $.ajax({
                    type: 'post',
                    url: "{:url('Assets/borrower')}",
                    data: {type:type,batch_number: batch_number,duration: duration,repayment_type: repayment_type,left_money_min: left_money_min,left_money_max: left_money_max},
                    dataType: 'json',
                    success: function (data) {
                        //console.log(data);
                        if (data.code == 1) {
                            //给table赋值
                            var  html = '';
                            for(var i=0;i<data.data.data.length;i++)
                            {
                                k = i + 1;
                                html = html+"<tr><td><input type='checkbox' class='checkbox' name='user_ids[]' value='"+data.data.data[i].id+"'>"+k+"</td>"+
                                        "<td>"+data.data.data[i].batch_number+"</td>"+
                                        "<td>"+data.data.data[i].company_name+"</td>"+
                                        "<td>"+data.data.data[i].telephone+"</td>"+
                                        "<td>"+data.data.data[i].money+"</td>"+
                                        "<td>"+data.data.data[i].left_duration+"</td>"+
                                        "<td>"+data.data.data[i].duration+"</td>"+
                                        "<td>"+data.data.duration+"</td>"+
                                        "<td>"+data.data.data[i].left_money+"</td>"+
                                        "<td>"+data.data.data[i].money+" </td></tr>";
                            }
                            $("#borrowerList").html(html); 
                            $("#interest_rate").val(data.data.interest_rate);
                            $("#interest_rate_base").val(data.data.interest_rate);//用作比较
                            $("#capital").val('');
                            $("#all_user_ids").removeAttr("checked");
                        } else {
                            $("#borrowerList").html('');
                            alert(data.message);
                        }
                    },
                    error: function (xhr, type) {
                        alert('Ajax error!')
                    }
                });
            });
        });
    </script>
</html>